// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')

const del = require('del')
const browerSync = require('browser-sync')

const loadPlugins = require('gulp-load-plugins')
const bs = browerSync.create()
const plugins = loadPlugins()

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
              
// 清除文件
const clean = () => {
  return del(['dist', 'temp'])
}
// 构建样式
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: 'expanded' }))
    .pipe(dest('temp'))
}
              
// 构建脚本
const scripts = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
}

// 构建模板
const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({data}))
    .pipe(dest('temp'))
}

// 构建图片
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// 构建字体文件
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// 构建public下的其他文件
const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('dist'))
}

const serve = () => {
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', scripts)
  watch('src/*.html', page)
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'src/public/**'
  ], bs.reload)

  bs.init({
    notify: false,
    port: 2080,
    open: false,
    files: 'temp/**',
    server: {
      baseDir: ['temp', 'src', 'public'], // 是数组时，先到第一个目录去找，找不到的话就会依次往后找
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

// 处理引用问题
const useref = () => {
  return src('temp/*.html', { base: 'temp' })
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({ 
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true
     })))
    .pipe(dest('dist'))
}

// 把所有需编译的任务放在一起。样式文件、js文件、html模板文件、图片编译、字体编译组合成一个并行任务
const compile = parallel(style, scripts, page)

// compile和extra组合成一个并行任务，该任务用于打包
// 为了保证每次打的包都是正确的，build中执行其他任务之前需要先执行clean
const build = series(
  clean, 
  parallel(
    series(compile, useref), 
    image, font, extra
  )
)

// 开发阶段先编译再启动服务器
const develop = series(compile, serve)

const start = develop

module.exports = {
  clean,
  serve,
  build,
  develop,
  start,
  useref
}


